#include "httpServer.hpp"
#include <memory>
using namespace std;
using namespace Server;

static void Usage(const string &proc)
{
    cout << "\nUsage:\t\n " << proc << endl
         << endl;
    _exit(1);
}
std::string suffixToDesc(const std::string &suffix)
{
    std::string ret = "Content-Type: ";
    if (suffix == ".html")
        ret += "text/html";
    else if (suffix == ".jpg")
        ret += "application/x-jpg";
    else 
        ret += "text/html";
    ret += "\r\n";
    return ret;
}
// 1.服务器与网页分离
// 2.url -> / ：web根目录
// 3.我们要正确的返回给客户端的资源类型，首先我们自己得知道！！！！
bool Get(const Request &req, Response &resp)
{
    // if(req.path == "/test.py")
    // {
    //     pipe -> fork -> execl 
    //     .......
         
    // }

    cout << "+++++++++++++http start++++++++++++++" << endl;
    cout << req.inbuffer;

    cout << "method: " << req.method << endl;
    cout << "url: " << req.url << endl;
    cout << "httpversion: " << req.httpversion << endl;

    cout << "path: " << req.path << endl;
    cout << "suffix: " << req.suffix << endl;
    cout << "size: " << req.size << endl;
    cout << "+++++++++++++http end++++++++++++++" << endl;


    std::string respline = "HTTP/1.1 200 OK\r\n";
    // std::string respline = "http/1.1 307 Temperary Redirect\r\n";
    

    // std::string respheader = "Content-Type: text/html\r\n"; // 根据Content-Type 对照表填写正文内容类型
    std::string respheader;
    //respheader += "Location: https://www.baidu.com/\r\n";
    respheader += suffixToDesc(req.suffix);
    if (req.size > 0)
    {
        respheader += "Content-Length: ";
        respheader += to_string(req.size);
        respheader += "\r\n";
    }
    respheader += "Set-Cookie: 123eq13abc; Max-Age=120\r\n";
    std::string respblank = "\r\n";
    //     std::string body = "<!DOCTYPE html>\
// <html>\
// <head>\
// <meta charset=\"utf-8\">\
// <title>for_test</title>\
// </head>\
// <body>\
//     <h1>我的第一个标题</h1>\
//     <p>我的第一个段落。</p>\
//     <a href=\"/test/a.html\">新闻</a>\
// </body>\
// </html>";
    std::string body;
    body.resize(req.size);
    // 如果读取失败，文件不存在，读取404文件内容
    if (!Util::readFile(req.path, const_cast<char *>(body.c_str()), req.size))
    {
        Util::readFile(heml_404, const_cast<char *>(body.c_str()), req.size); // 一定会调用成功
    }
    // respheader += "Content-Length: ";
    // respheader += to_string(body.size());
    // respheader += "\r\n";
    resp.outbuffer = respline;
    resp.outbuffer += respheader;
    resp.outbuffer += respblank;
    cout << "+++++++++++++response start++++++++++++++" << endl;

    cout << resp.outbuffer;
    cout << "+++++++++++++response end++++++++++++++" << endl;

    resp.outbuffer += body;
    return true;
}




int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
    }
    uint16_t port = atoi(argv[1]);
    unique_ptr<HttpServer> httpsvr(new HttpServer(Get, port));

    httpsvr->initServer();
    httpsvr->registerCb("./wwwroot/index.html", Get);
    httpsvr->start();
    return 0;
}
